/*
============================================================================
Name        : BasePage.cpp
Author      : 
Version     :
Copyright   : Your copyright notice
Description : BasePage.cpp - header file
============================================================================
*/

#include "BasePage.h"
#include "gameApp.h"
#include "math.h"

CXFolder::CXFolder(const char* name,CXFolder *father,int16 type,int16 icon,bool isCanIn)
{
	m_Index=0;
	m_Drawpos=0;
	m_Type=type;
	m_Icon=icon;
	m_IsCanIn=isCanIn;
	m_Father=father;
	m_Name.Copy(name);
}

CXFolder::CXFolder(const CXString& name,CXFolder *father,int16 type,int16 icon,bool isCanIn)
{
	m_Index=0;
	m_Drawpos=0;
	m_Type=type;
	m_Icon=icon;
	m_IsCanIn=isCanIn;
	m_Father=father;
	m_Name.Copy(name);
}

CXFolder::~CXFolder()
{
	DelAll();
}

CXFolder * CXFolder::AddNewSub(const char* name,int16 type,int16 icon,bool isCanIn,int16 pos)
{
	CXFolder *newfolder=new CXFolder(name,this,type,icon,isCanIn);
	m_Subfolder.AddData(newfolder,pos);
	return newfolder;
};

CXFolder * CXFolder::AddNewSub(const CXString& name,int16 type,int16 icon,bool isCanIn,int16 pos)
{
	CXFolder *newfolder=new CXFolder(name,this,type,icon,isCanIn);
	m_Subfolder.AddData(newfolder,pos);
	return newfolder;
};

CXFolder * CXFolder::AddSubLink(CXFolder *sub,int16 pos)
{
	m_Subfolder.AddData(sub,pos);
	return sub;
};

void CXFolder::DelSub(int16 index)
{
	if(m_Subfolder.GetData(index)->m_Father==this)
		m_Subfolder.Delete(index);
	else 
		m_Subfolder.Remove(index);
}

void CXFolder::DelSub(CXFolder *folder)
{
	for (int i=0;i<m_Subfolder.Count();i++)
	{
		if (folder==m_Subfolder.GetData(i))DelSub(i);
	}
}

void CXFolder::DelAll()
{
	for (int i=m_Subfolder.Count()-1;i>=0;i--)
	{
		if(m_Subfolder.GetData(i)->m_Father==this)
			m_Subfolder.Delete(i);
		else
			m_Subfolder.Remove(i);
	}
}

ScollObj::ScollObj()
{
	m_ShakeSpeed = 0;
	m_MoveSpeed  = 0;
	m_DragSpeed  = 0;
	m_DownPos  = -1;
	m_LastPos  = 0;
}

ScollObj::~ScollObj()
{
}

int ScollObj::DealScoll(int basePos,int starPos,int endPos,int maxScoll,int dir,float speed,bool isCenter,_Rect * touch)
{
	int finalPos = basePos;
	int clipHeight = endPos - starPos;
	int maxscoll = maxScoll - clipHeight;
	int touchState = CGameApp::GetTouchInput()->GetEvent();
	int nowPos = 0;
	int dragSpeed = 0;
	static int dragdir = 0;
	if(dir > 0)
	{
		dragSpeed = CGameApp::GetTouchInput()->GetVelocity().y.Int16();
		nowPos  = CGameApp::GetTouchInput()->GetPosition().y.Int16();
	}
	if(dir < 0)
	{
		dragSpeed = CGameApp::GetTouchInput()->GetVelocity().x.Int16();
		nowPos  = CGameApp::GetTouchInput()->GetPosition().x.Int16();
	}
	if(speed > 0)dragSpeed *= speed;
	
	if(maxscoll < 0)maxscoll = 0;
	
	if(dragdir == 0 || dragdir == dir)
	{
		if(!touch || CGameApp::GetTouchInput()->PressRect(touch))
		{
			if(touchState==TOEV_DOWN)
			{
				m_DownPos = nowPos;
				m_LastPos = basePos;
				m_ShakeSpeed = m_MoveSpeed = 0;
			}
			if(touchState==TOEV_MOVE)
			{
				if(m_DownPos == -1)
				{
					m_DownPos = nowPos;
					m_LastPos = basePos;
					m_ShakeSpeed = m_MoveSpeed = 0;
				}
				if(dragdir == 0)
				{
					int16 dirVX = CGameApp::GetTouchInput()->GetVelocity().x.Int16();
					int16 dirVY = CGameApp::GetTouchInput()->GetVelocity().y.Int16();
					if(fabs(dirVX) < fabs(dirVY))dragdir = 1;
					if(fabs(dirVX) > fabs(dirVY))dragdir = -1; 
					if(dragdir != dir)dragdir = 0;
				}
			}
		}
		if(!touch || CGameApp::GetTouchInput()->HitsRect(touch))
		{
			if(touchState==TOEV_UP && m_DownPos == -1)
			{
				m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = dragSpeed;
				CGameApp::GetTouchInput()->GetVelocity().Clear();
			}			
		}
	}
	else 
	{
		m_DownPos = -1;
		m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = 0;
	}
	
	if(m_DownPos != -1)
	{
		if(touchState==TOEV_UP)
		{
			dragdir = 0;
			m_DownPos = -1;
			m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = dragSpeed;
			CGameApp::GetTouchInput()->GetVelocity().Clear();
		}
		else 
		{
			int dragPos = nowPos - m_DownPos;
			finalPos = m_LastPos + dragPos;
			m_ShakeSpeed = m_MoveSpeed = 0;
		}
	}		
	finalPos += m_MoveSpeed;
	
	if(m_MoveSpeed != 0)
	{
		if(m_MoveSpeed > 0)m_MoveSpeed--;
		if(m_MoveSpeed < 0)m_MoveSpeed++;	
	}
	if(finalPos > starPos)
	{
		finalPos = finalPos + starPos >> 1;
		m_MoveSpeed -= m_ShakeSpeed/10;
		if(m_MoveSpeed < 0)
		{
			m_MoveSpeed = 0;
			m_ShakeSpeed = 0;
		}
	}
	if(finalPos < -maxscoll + starPos)
	{
		finalPos = -maxscoll + starPos - (-maxscoll + starPos - finalPos >> 1);
		m_MoveSpeed -= m_ShakeSpeed/10;
		if(m_MoveSpeed > 0)
		{
			m_MoveSpeed = 0;
			m_ShakeSpeed = 0;
		}
	}
	
	if(isCenter && (fabs(m_MoveSpeed) < clipHeight/16 || speed == 0) && 
	   m_DownPos == -1 && (finalPos < starPos && finalPos > starPos - maxscoll))
	{
		m_MoveSpeed = 0;
		int dMove = (starPos - finalPos) % clipHeight;
		int dStep = 5;
		if(dMove != 0)
		{
			int scolldir = 0;
			if(speed == 0 && m_DragSpeed > 5)scolldir = 1;
			else if(speed == 0 && m_DragSpeed < -5)scolldir = -1;
			else if(dMove < (clipHeight >> 1))scolldir = 1;
			else scolldir = -1;
			
			if(scolldir == 1)
			{
				if(dMove <= dStep)finalPos += dMove;
				else finalPos += (dMove / dStep);
			}
			else if(scolldir == -1)
			{
				if(clipHeight - dMove <= dStep)finalPos -= clipHeight - dMove;
				else finalPos -= ((clipHeight - dMove) / dStep);
			}
		}
	}
    if(finalPos > starPos)
	{
        finalPos = starPos;
	}
	if(finalPos < -maxscoll + starPos)
	{
        finalPos = -maxscoll + starPos;
	}
	return finalPos;
}

/*
int ScollObj::DealScoll(int basePos,int starPos,int endPos,int maxScoll,int dir,float speed,bool isCenter,_Rect * touch)
{
	int finalPos = basePos;
	int clipHeight = endPos - starPos;
	int maxscoll = maxScoll - clipHeight;
	int touchState = CGameApp::GetTouchInput()->GetEvent();
	int nowPos = 0;
	int dragSpeed = 0;
	static int dragdir = 0;
	if(dir > 0)
	{
		dragSpeed = CGameApp::GetTouchInput()->GetVelocity().y.Int16();
		nowPos  = CGameApp::GetTouchInput()->GetPosition().y.Int16();
	}
	if(dir < 0)
	{
		dragSpeed = CGameApp::GetTouchInput()->GetVelocity().x.Int16();
		nowPos  = CGameApp::GetTouchInput()->GetPosition().x.Int16();
	}
	if(speed > 0)dragSpeed *= speed;
	
	if(maxscoll < 0)maxscoll = 0;
	
	if(dragdir == 0 || dragdir == dir)
	{
		if(!touch || CGameApp::GetTouchInput()->PressRect(touch))
		{
			if(touchState==TOEV_DOWN)
			{
				m_DownPos = nowPos;
				m_LastPos = basePos;
				m_ShakeSpeed = m_MoveSpeed = 0;
			}
			if(touchState==TOEV_MOVE)
			{
				if(m_DownPos == -1)
				{
					m_DownPos = nowPos;
					m_LastPos = basePos;
					m_ShakeSpeed = m_MoveSpeed = 0;
				}
				if(dragdir == 0)
				{
					int16 dirVX = CGameApp::GetTouchInput()->GetVelocity().x.Int16();
					int16 dirVY = CGameApp::GetTouchInput()->GetVelocity().y.Int16();
					if(fabs(dirVX) < fabs(dirVY))dragdir = 1;
					if(fabs(dirVX) > fabs(dirVY))dragdir = -1;
					if(dragdir != dir)dragdir = 0;
				}
			}
		}
		if(!touch || CGameApp::GetTouchInput()->HitsRect(touch))
		{
			if(touchState==TOEV_UP && m_DownPos == -1)
			{
				m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = dragSpeed;
				CGameApp::GetTouchInput()->GetVelocity().Clear();
			}			
		}
	}
	else 
	{
		m_DownPos = -1;
		m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = 0;
	}
	
	if(m_DownPos != -1)
	{
		if(touchState==TOEV_UP)
		{
			dragdir = 0;
			m_DownPos = -1;
			m_ShakeSpeed = m_MoveSpeed = m_DragSpeed = dragSpeed;
			CGameApp::GetTouchInput()->GetVelocity().Clear();
		}
		else 
		{
			int dragPos = nowPos - m_DownPos;
			finalPos = m_LastPos + dragPos;
			m_ShakeSpeed = m_MoveSpeed = 0;
		}
	}		
	finalPos += m_MoveSpeed;
	
	if(m_MoveSpeed != 0)
	{
		if(m_MoveSpeed > 0)m_MoveSpeed--;
		if(m_MoveSpeed < 0)m_MoveSpeed++;	
	}
	if(finalPos > starPos)
	{
		finalPos = finalPos + starPos >> 1;
		m_MoveSpeed -= m_ShakeSpeed/10;
		if(m_MoveSpeed < 0)
		{
			m_MoveSpeed = 0;
			m_ShakeSpeed = 0;
		}
	}
	if(finalPos < -maxscoll + starPos)
	{
		finalPos = -maxscoll + starPos - (-maxscoll + starPos - finalPos >> 1);
		m_MoveSpeed -= m_ShakeSpeed/10;
		if(m_MoveSpeed > 0)
		{
			m_MoveSpeed = 0;
			m_ShakeSpeed = 0;
		}
	}
	
	if(isCenter && (fabs(m_MoveSpeed) < clipHeight/16 || speed == 0) && 
	   m_DownPos == -1 && (finalPos < starPos && finalPos > starPos - maxscoll))
	{
		m_MoveSpeed = 0;
		int dMove = (starPos - finalPos) % clipHeight;
		int dStep = 5;
		if(dMove != 0)
		{
			int scolldir = 0;
			if(speed == 0 && m_DragSpeed > 5)scolldir = 1;
			else if(speed == 0 && m_DragSpeed < -5)scolldir = -1;
			else if(dMove < (clipHeight >> 1))scolldir = 1;
			else scolldir = -1;
			
			if(scolldir == 1)
			{
				if(dMove <= dStep)finalPos += dMove;
				else finalPos += (dMove / dStep);
			}
			else if(scolldir == -1)
			{
				if(clipHeight - dMove <= dStep)finalPos -= clipHeight - dMove;
				else finalPos -= ((clipHeight - dMove) / dStep);
			}
		}
	}
	
	return finalPos;
}*/

CPageManager::CPageManager()
{
	m_PageFrom = NULL;
	m_PageTo   = NULL;
	m_CurPage  = NULL;

	m_isSwitching = false;
	m_isRelCurPage = false;
}

CPageManager::~CPageManager()
{
	DelAllPage();
}

CBasePage * CPageManager::AddPage(CBasePage *page,int16 pageid)
{
	page->m_PageId = pageid;
	page->m_isCreate = false;
	page->m_PageFrom = NULL;
	page->m_ScollIndex = 0;
	m_AllPage.AddData(page);
	return page;
}

CBasePage * CPageManager::GetPage(int16 pageid)
{
	CBasePage * backpage = NULL;
	for (int i=m_AllPage.Count()-1;i>=0;i--)
	{
		if(m_AllPage.GetData(i)->m_PageId == pageid)
		{
			backpage = m_AllPage.GetData(i);
			break;
		}
	}
	return backpage;
}

void CPageManager::DelPage(int16 pageid)
{
	for (int i=m_AllPage.Count()-1;i>=0;i--)
	{
		CBasePage *page = m_AllPage.GetData(i);
		if(page->m_PageId == pageid)
		{
			if(page->m_isCreate)
			{
				if(page == m_CurPage)
				{
					page->UnLoad();
				}
				page->Release();
				page->m_isCreate = false;
			}
			m_AllPage.Delete(i);
			break;
		}
	}
}

void CPageManager::DelAllPage()
{
	for (int i=m_AllPage.Count()-1;i>=0;i--)
	{
		CBasePage *page = m_AllPage.GetData(i);
		if(page->m_isCreate)
		{
			if(page == m_CurPage)
			{
				page->UnLoad();
			}
			page->Release();
			page->m_isCreate = false;
		}
		m_AllPage.Delete(i);
	}
}

void CPageManager::GotoPage(int16 pageid,bool isRelCurPage)
{
	m_PageFrom = m_CurPage;
	m_PageTo   = GetPage(pageid);
	if (m_PageTo && m_PageFrom != m_PageTo)
	{
		m_isSwitching = true;
		m_isRelCurPage = isRelCurPage;
		m_PageTo->m_PageFrom = m_PageFrom;
		
		if(!m_PageTo->m_isCreate)
		{
			m_PageTo->Create();
			m_PageTo->m_isCreate=true;
			m_PageTo->m_ScollIndex=0;
		}
		m_PageTo->Load();
	}
}

void CPageManager::Back(bool isRelCurPage)
{
	m_PageFrom = m_CurPage;
	m_PageTo   = m_CurPage->m_PageFrom;
	if (m_PageTo)
	{
		m_isSwitching = true;
		m_isRelCurPage = isRelCurPage;
		
		if(!m_PageTo->m_isCreate)
		{
			m_PageTo->Create();
			m_PageTo->m_isCreate=true;
			m_PageTo->m_ScollIndex=0;
		}
		m_PageTo->Load();
	}
}

void CPageManager::SwitchFin()
{
	m_isSwitching = false;
	if (m_PageFrom)
	{
		m_PageFrom->UnLoad();
		if(m_isRelCurPage)
		{
			m_PageFrom->Release();
			m_PageFrom->m_isCreate=false;
		}
	}
	m_CurPage = m_PageTo;
}

